﻿/*
* ItemManage.js
* create by shzhang 2016.03.25
*/
$(function () {
    //announcement
    var divMsg = new jSharp.popUpDiv('divMsg');
    var divAdd = new jSharp.popUpDiv('divAdd');
    var divUploading = new jSharp.popUpDiv('divUploading');
    var filePath = "../UploadFile/";

    //Xss反注入
    function filterString(s, maxLen) {
        if (s.length > maxLen) return false;
        s = s.replace(/ /g, '');
        var param = new Array('<', '%3c', '>', '%3e', 'script', 'document', 'eval', '/', '%2f', '//');
        for (var i = 0; i < param.length; i++) {
            var reg = new RegExp(param[i]);
            if (reg.test(s)) return false;
        }
        return true;
    }
    function tips(s) { //消息提示
        divMsg.Show();
        $('#msgContent').html(s);
    }
    $('#btnUpload').click(function () {
        divUploading.Show();
        $.ajaxFileUpload({
            url: '../Common/Upload.aspx',
            secureuri: false,
            dataType: 'json',
            fileElementId: 'txtFile',
            success: function (data) {
                divUploading.Close();
                if (data.fileType == "unknow") {
                    tips('文件格式不正确，目前支持的文件格式包括 .mp4 .jpg .png，且大小不能超过40MB');
                    return;
                }
                if (data.fileType == "pic") {
                    //按格式显示
                    fileType = "1";
                    var fullName = filePath + data.fileName;
                    console.log(data.fileName);
                    console.log($("[id$='hdfImg']"));
                    $('[id$="imgPrev"]').attr("src", fullName);
                    $("[id$='hdfImg']").attr("value", data.fileName);
                    tips("上传完成");
                    var t = setTimeout(function () {
                        divMsg.Close();
                        clearTimeout(t);
                    }, 1000)
                }
                if (data.error == "wrong type") {
                    tips("请先选择文件，并确保格式正确！");
                }
            }
        })
    })

    $('#hideMsg').click(function () {
        divMsg.Close();
    })
    $('#hideAdd').click(function () {
        divAdd.Close();
    })
    $('#btnAddItem').click(function () {
        divAdd.Show();
    })
} ())
